import nodemailer from 'nodemailer'
import { Response } from 'express'
interface IEmail {
    service: string
    port: number
    secureConnection: boolean
    auth: {
        user: string
        pass: string
    }
}
const USER: string = 'star834526069@163.com'
export const EMAIL: IEmail = {
    service: '163',   // 使用163邮箱
    port: 465, // 对于SSL连接，端口通常是465
    secureConnection: true, // 使用SSL
    auth: {
        user: USER, // 邮箱账号
        pass: 'WTib334ePc79bxFd'    // 邮箱授权码
    }
}

interface IEmailResponse {
    code: number
    message: string
    data: any
}
// 创建邮件发送的配置
const transporter = nodemailer.createTransport(EMAIL);

const emailSync = (res: Response, email: string): void => {
    // 发送邮件
    return  transporter.sendMail({
        from: USER, // 发送者
        to: email, // 接收者
        subject: 'App验证码', // 邮件主题
        text: '邮件内容', // 邮件内容
        html: '<div><div>您的验证码为：<div><center style="font-size: 18px;font-weight: bold">123456</center></div></div></div>', // 邮件内容，支持HTML
    }, (error: any, info: any) => {
        const response: IEmailResponse = error ? { code: 500, message: '失败', data: error } : { code: 200, message: '成功', data: info }
        res.send(response)
    });
}

const emailSend = (res: any, email: string) => {
    return emailSync(res, email)
}

export default emailSend